package com.lyc.dao;

import com.lyc.entity.Product;
import org.apache.solr.client.solrj.SolrClient;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.request.SolrPing;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.SolrDocument;
import org.apache.solr.common.SolrDocumentList;
import org.apache.solr.common.SolrInputDocument;
import org.apache.solr.common.StringUtils;
import org.apache.solr.common.util.NamedList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Repository("solrDao")
public class SolrDaoImpl implements ISolrDao {

    @Autowired
    private SolrClient solrClient;

    @Override
    public List<Product> queryProducts(SolrQuery solrQuery) throws IOException, SolrServerException {
        QueryResponse queryResponse = solrClient.query(solrQuery);
        SolrDocumentList documentList = queryResponse.getResults();
        //获取查询结果总数
        List<Product> productList = new ArrayList<>();
        for (SolrDocument document : documentList) {
            Product product = new Product();
            //避免空指针使程序停止
            if (document.get("id") != null && !StringUtils.isEmpty(document.get("id").toString())) {
                product.setPid(Integer.parseInt(document.get("id").toString()));
            }
            if (document.get("product_name") != null && !StringUtils.isEmpty(document.get("product_name").toString())) {
                product.setName(document.get("product_name").toString());
            }
            //高亮显示关键字
            Map<String, Map<String, List<String>>> highlighting = queryResponse.getHighlighting();
            if (highlighting != null && highlighting.size() > 0) {
                if (document.get("id") != null && !StringUtils.isEmpty(document.get("id").toString()) && document.get("product_name") != null && !StringUtils.isEmpty(document.get("product_name").toString())) {
                    List<String> list = highlighting.get(document.get("id")).get("product_name");
                    if (list != null && list.size() > 0)
                        product.setName(list.get(0));
                }
            }
            if (document.get("product_price") != null && !StringUtils.isEmpty(document.get("product_price").toString())) {
                product.setPrice(Double.valueOf(document.get("product_price").toString()));
            }
            if (document.get("product_off_price") != null && !StringUtils.isEmpty(document.get("product_off_price").toString())) {
                product.setOffPrice(Double.valueOf(document.get("product_off_price").toString()));
            }
            if (document.get("product_sale") != null && !StringUtils.isEmpty(document.get("product_sale").toString())) {
                product.setSale(Integer.valueOf(document.get("product_sale").toString()));
            }
            if (document.get("product_picture") != null && !StringUtils.isEmpty(document.get("product_picture").toString())) {
                product.setUrl(document.get("product_picture").toString());
            }
            if (document.get("product_freight") != null && !StringUtils.isEmpty(document.get("product_freight").toString())) {
                product.setFreight(Double.valueOf(document.get("product_freight").toString()));
            }
            if (document.get("product_quality") != null && !StringUtils.isEmpty(document.get("product_quality").toString())) {
                product.setQuality(document.get("product_quality").toString());
            }
            if (document.get("product_store") != null && !StringUtils.isEmpty(document.get("product_store").toString())) {
                product.setStore(Integer.valueOf(document.get("product_store").toString()));
            }
            productList.add(product);
        }
        return productList;
    }

    @Override
    public void test() throws IOException, SolrServerException {
        NamedList<Object> namedList = solrClient.request(new SolrPing());
        System.out.println(namedList);
    }

    @Override
    public void deleteById(String pid) {
        try {
            solrClient.deleteById(pid);
            solrClient.commit();//不提交，删除会回滚！
        } catch (SolrServerException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 如果id值存在索引则删除再添加，否则直接添加
     * @param product
     */
    @Override
    public void addOrUpdateProduct(Product product) {
        //创建solr文档对象
        SolrInputDocument doc = new SolrInputDocument();
        //注意：1.域要先定义后使用；2.必须要有id主键域
        doc.addField("id", product.getPid());//
        doc.addField("product_name", product.getName());
        doc.addField("product_price", product.getPrice());
        doc.addField("product_off_price", product.getOffPrice());
        doc.addField("product_store", product.getStore());
        doc.addField("product_sale", product.getSale());
        doc.addField("product_picture", product.getUrl());
        doc.addField("product_date", product.getDate());
        doc.addField("product_quality", product.getQuality());
        doc.addField("product_freight", product.getFreight());
        doc.addField("product_category_id", product.getCid());
        try {
            //使用客户端添加/更新文档操作
            solrClient.add(doc);//?
            //提交
            solrClient.commit();
        } catch (SolrServerException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
